﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.DataControls;
using Acms.Bll;

public partial class Templates_Controls : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lnkAdd.Click += new EventHandler(lnkAdd_Click);
        dsControls.SelectData += new EventHandler<SelectEventArgs<Acms.Bll.TemplateControl>>(dsControls_SelectData);
        dsControlTypes.SelectData += new EventHandler<SelectEventArgs<ControlType>>(dsControlTypes_SelectData);
        dsControls.UpdatingData += new EventHandler<UpdatingDataEventArgs<Acms.Bll.TemplateControl>>(dsControls_UpdatingData);
        grid.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(grid_ItemCommand);
        if (!IsPostBack)
        {
            this.Title = string.Format("{0} Template Controls", base.CurrentTemplate.Name);
        }
    }

    void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
        {
            int change = 1;
            if (e.CommandName == "MoveUp")
            {
                change = -1;
            }
            List<Acms.Bll.TemplateControl> tmp = Acms.Bll.TemplateControl.GetAllByTemplateId(TemplateId);

            Acms.Bll.TemplateControl.Move(tmp, e.Item.DataSetIndex, e.Item.DataSetIndex + change);
            grid.Rebind();
            grid.SelectedIndexes.Clear();
            grid.SelectedIndexes.Add(e.Item.ItemIndex + change);
        }
    }

    void dsControls_UpdatingData(object sender, UpdatingDataEventArgs<Acms.Bll.TemplateControl> e)
    {
        if (e.Action == UpdateAction.Insert)
        {
            e.Item.TemplateId = this.TemplateId;
        }
    }

    void lnkAdd_Click(object sender, EventArgs e)
    {
        grid.MasterTableView.IsItemInserted = true;
        grid.Rebind();
    }

    void dsControlTypes_SelectData(object sender, SelectEventArgs<ControlType> e)
    {
        e.SetData(ControlType.GetAllBySiteId(AcmsContext.SiteId));
    }

    void dsControls_SelectData(object sender, SelectEventArgs<Acms.Bll.TemplateControl> e)
    {
        e.SetData(Acms.Bll.TemplateControl.GetAllByTemplateId(TemplateId));
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Controls");
    }
}

